package com.huayun.plugin.fastenerLibrary.service;
import com.huayun.plugin.fastenerLibrary.dao.FastenerLibraryRepository;
import com.huayun.plugin.fastenerLibrary.domain.FastenerLibrary;

import com.huayun.plugin.fastenerWeightLibrary.domain.FastenerWeightLibrary;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

;

@Service
public class FastenerLibraryService {
    @Autowired
    private final FastenerLibraryRepository fastenerLibraryRepository;
    private final Logger logger = LoggerFactory.getLogger(FastenerLibraryService.class);

    public FastenerLibraryService(FastenerLibraryRepository fastenerLibraryRepository) {
        this.fastenerLibraryRepository = fastenerLibraryRepository;
    }

    @Operation(summary = "文件类型转成XMl文档")
    public static Document convertXML(MultipartFile multipartFile) throws Exception {
        try {
            // 1. 读取 MultipartFile 的内容
            byte[] bytes = multipartFile.getBytes();

            // 2. 将内容解析为 XML 格式
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(bytes));

            // 3. 返回 Document 对象
            return document;
        } catch (Exception e) {
            return null;
        }
    }

    public List<FastenerLibrary> getPartInfo(String PartNumber){
        List<FastenerLibrary> numberContains = fastenerLibraryRepository.findByPartNumberContaining(PartNumber);
        return numberContains;
    }

    @Operation(summary = "导入:上传解析的 xml属性文件")
    public boolean importPropertyFile(MultipartFile file) {
        boolean result = false;
        try {
            org.w3c.dom.Document document = convertXML(file);
            if (document.hasChildNodes()) {
                Element root = document.getDocumentElement();
                if (root.getNodeName().equals("RAttributes")) {
                    //解析根节点下的一级节点
                    if (root.hasChildNodes()) {
                        NodeList allNodesList = root.getChildNodes();
                        for (int i = 0; i < allNodesList.getLength(); i++) {
                            Node attNode = allNodesList.item(i);
                            if (attNode.getNodeName().equals("fastener")) {
                                if (attNode.hasChildNodes()) {
                                    NodeList attList = attNode.getChildNodes();

                                    Element elementCur = (Element)attNode;
                                    String partNumber = elementCur.getAttribute("partNumber");
                                    List<FastenerLibrary> listFastenerInfo = getPartInfo(partNumber);
                                    if(listFastenerInfo.size() != 0)
                                    {
                                        continue;
                                    }

                                    String length = "";
                                    String weight = "";
                                    String material = "";
                                    String density = "";
                                    String fastenerPartNumber = "";

                                    for (int j = 0; j < attList.getLength(); j++) {
                                        Node curAtt = attList.item(j);
                                        if (curAtt.getNodeName().equals("weight")) {
                                            weight = curAtt.getTextContent();
                                        } else if (curAtt.getNodeName().equals("length")) {
                                            length = curAtt.getTextContent();
                                        } else if (curAtt.getNodeName().equals("material")) {
                                            material = curAtt.getTextContent();
                                        } else if (curAtt.getNodeName().equals("density")) {
                                            density = curAtt.getTextContent();
                                        }else if (curAtt.getNodeName().equals("fastenerPartNumber")) {
                                            fastenerPartNumber = curAtt.getTextContent();
                                        }
                                    }
                                    FastenerLibrary fastenerLibrary = new FastenerLibrary();
                                    fastenerLibrary.setDensity(Double.parseDouble(density));
                                    fastenerLibrary.setMaterial(material);
                                    fastenerLibrary.setLength(Double.parseDouble(length));
                                    fastenerLibrary.setWeight(Double.parseDouble(weight));
                                    fastenerLibrary.setPartNumber(partNumber);
                                    fastenerLibrary.setFastenerPartNumber(fastenerPartNumber);

                                    fastenerLibraryRepository.insert(fastenerLibrary);
                                }
                            }
                        }
                        result = true;
                    }
                }
            }
        } catch (Exception e) {

        } finally {
            return result;
        }
    }
    //获取
}
